/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import BO.Service;
import ws.UserUI;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.view.facelets.FaceletContext;
import javax.servlet.http.HttpSession;

public class SendMessageBean {

    private String message;
    private String receivername;

    public SendMessageBean() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getReceivername() {
        return receivername;
    }

    public void setReceivername(String receivername) {
        this.receivername = receivername;
    }

    public String sendMessage() {

        /*AutoCompleteBean backBean = (AutoCompleteBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("autoCompleteBean");
        receivername = backBean.getText();*/

        
        //FacesContext context = FacesContext.getCurrentInstance();
        //AutoCompleteBean autoCompleteBean = (AutoCompleteBean) context.getELContext().getELResolver().getValue(context.getELContext(), null, context);
        //AutoCompleteBean autoCompleteBean = (AutoCompleteBean) context.getApplication().evaluateExpressionGet(context, "#{autoCompleteBean}", AutoCompleteBean.class);
        //receivername = autoCompleteBean.getText();
         

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user = (UserUI) session.getAttribute("User");
        if (Service.sendPrivateMessage(receivername, message, user.getId())) {
            return "true";
        }
        return "false";
    }
    
    public List<String> complete(String query) {
        List<String> results = new ArrayList<String>();
        
        results = Service.getUserNamesLike(query);
        
        

        return results;
    }
}
